//
//  DDSetting.m
//  dingdang
//
//  Created by lichq on 7/24/15.
//  Copyright (c) 2015 阮 沧晖. All rights reserved.
//

#import "DDSetting.h"
#import "DDFeekBack.h"
#import "DDAbout.h"
#import "DDShare.h"
#import "DDNetWorkAPIClient.h"
#import "DDShareViewController.h"
@interface DDSetting (){
    UILabel *labVersion;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation DDSetting

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self createUI];
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"设置", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.001)];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"意见反馈";
            break;
        case 1:
            cell.textLabel.text = @"分享好友";
            break;
        case 2:
            cell.textLabel.text = @"关于";
            break;
        case 3:
        {
            cell.textLabel.text = @"版本检查";
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, 80, 20)];
            label.textColor = [UIColor lightGrayColor];
            label.textAlignment = NSTextAlignmentRight;
            NSString *versionHighest = @"1"; //TODO
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            CFShow((__bridge CFTypeRef)(infoDictionary));
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            if ([app_Version isEqualToString:versionHighest]) {
                app_Version = @"最新版本";
            }
            label.text = app_Version;
            cell.accessoryView = label;
            labVersion = label;
            break;
        }
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            DDFeekBack *vc = [[DDFeekBack alloc]initWithNibName:@"DDFeekBack" bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1:
        {
           // DDShare *vc = [[DDShare alloc]initWithNibName:@"DDShare" bundle:nil];
           // [self.navigationController pushViewController:vc animated:YES];
            [DDShareViewController  showShareWithController:self];
            break;
        }
        case 2:
        {
            DDAbout *vc = [[DDAbout alloc]initWithNibName:@"DDAbout" bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 3://TODO
        {
            [self checkVersion];
            break;
        }
            
        default:
            break;
    }
}

- (void)checkVersion{ //TODO
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network getCheckVersion:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        NSLog(@"%@", message);
        if (isSuccess) {
            [[[UIAlertView alloc]initWithTitle:@"当前已是最新版" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            NSString *versionHighest = @"最新版本"; //TODO
            labVersion.text = versionHighest;
        }
    }];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
